﻿using System;
using Microsoft.Extensions.DependencyInjection;
using netCoreStudy.Quartz;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;

namespace netCoreStudy.Extensions
{
	/// <summary>
	/// QuartZ 服务 设置
	/// </summary>
	public static class QuartZSetup
	{
		public static void AddQuartZSetup(this IServiceCollection services)
		{
			if (services == null) throw new ArgumentNullException(nameof(services));

			// //自己定义的后台服务 
			// services.AddHostedService<ServerHostedService>();
			// //将quartZ作为后台服务 
			// services.AddHostedService<QuartzHostedService>();

			//添加Quartz服务
			services.AddSingleton<IJobFactory, QuartJobFactory>();
			services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
			//添加我们的Job
			services.AddSingleton<SpiderPageJob>();
			// 0/10 * * * * ? 每10秒执行一次
			// 0 0 */1 * * ? 每1小时执行一次
			services.AddSingleton(
				new JobSchedule(typeof(SpiderPageJob), "0 0 0/1 * * ?")
				// new JobSchedule(typeof(SpiderPageJob), "0/10 * * * * ?")
			);
		}
	}
}